package games.swingPlus;

import hardware.Display2D;



public class PlayField extends graphics.Bitmap2D {

	public static short LEVER_COLOR =0xfff;
	
	private static final byte[] [] levelData = {{0,0,0,0,0,0,0,0},
												{0,1,0,0,0,0,0,0},										
												{0,1,0,0,0,0,1,0},
												{0,1,1,1,0,0,0,0},
												{0,1,0,1,0,1,0,0}, 
												{0,1,2,1,0,1,0,0}, 
												{0,2,0,0,0,0,1,0},
												{1,2,1,0,0,1,2,1},};

	
	
	public PlayField() {
	//	super(Display2D.getColumnCount(), Display2D.getRowCount()-3);
		super(8, 5);
	}

	/**
	 * @author Tavin
	 * 
	 * date 2011.05.16
	 * 
	 * initialize  play field, set positions and heights of levers
	 * 
	 * @param level - actual level 
	 */
	
	
	public void init(int level) {

		
		int xCounter;
		int yCounter;
		
		fill((short)0x000);
	
		level-=1;
		
		if (level>=(levelData.length-1))
			level=levelData.length-1;
		
		for (xCounter=0;xCounter<Display2D.getColumnCount() ;xCounter++)
			for (yCounter=0;yCounter< levelData[level] [xCounter];yCounter++)
				{
				setPixel(xCounter, yCounter, LEVER_COLOR);				
				}

	}

	/**
	 * @author Tavin
	 * 
	 *  date 2011.05.16
	 */
	
	public void draw() {

		Display2D._display2DBitmap(0, 0, data);
	}

	
	/**
	 * @author Tavin
	 *  date 16.05.2011
	 *  
	 *  
	 *  
	 * @param load
	 * @param xPos  
	 * @return 
	 */
	public boolean push(short load, int xPos) {

		int yPos;
		
		yPos=this.getYSize()-1;
		
	if (getPixel(xPos,yPos)!=0x000){
		return(true);
	}

		setPixel(xPos, yPos, load);
		
		return(false);
	}

	
	
	public int process() {
		int xCounter;
		int yCounter;
		short value;
		int points=0;
		
		for (yCounter=1;yCounter<getYSize();yCounter++)
			for (xCounter=0;xCounter<getXSize();xCounter++)
			{
				value= getPixel(xCounter,yCounter);
				
				if ((value !=0) && (getPixel(xCounter,yCounter-1)==0)){
					setPixel(xCounter,yCounter-1,value);
					setPixel(xCounter,yCounter,(short)0);
				}
			}
		
		balanceLevers();
		

		points=checkMatches();

	return(points);	
	}

	/**
	 * @author Tavin
	 * 
	 * Date 2011.05.16
	 * 
	 * check if all levers are perfectly balanced, if not balance levers 
	 */
	
public void balanceLevers()
{
	int xCounter;
	int yCounter;
	int xHalf=getXSize()/2;
	int xExtend=getXSize()-1;
	
	int weight;
	int counterWeight;
	
	int ySize =getYSize();
	
	
//	for (xCounter=0;xCounter<xHalf;xCounter++)
	for (xCounter=0;xCounter<xExtend;xCounter++)
		{
		weight=0;
		counterWeight=0;
		
		if (getPixel(xCounter,0)==LEVER_COLOR) 
			{
			for (yCounter=0;yCounter<ySize;yCounter++)
				{
				if (getPixel(xCounter,yCounter)>0)
					{
					weight++;
					}
				
				if (getPixel(xExtend-xCounter,yCounter)>0)
					{
					counterWeight++;
					}
				}
				
			if ((weight>(counterWeight+1)) && (getPixel(xCounter,0)==LEVER_COLOR) )
				{
				popLever(xCounter);
				pushLever(xExtend-xCounter);
				}

			if (((weight+1)<counterWeight) && (getPixel(xExtend-xCounter,0)==LEVER_COLOR)) 
				{
				popLever(xExtend-xCounter);
				pushLever(xCounter);
				}
		}
	}

}
	
	/**
	 * @author Tavin
	 * 
	 * Date 2011.05.16
	 * 
	 * pop lever on given x position, lever an all balls are moved in y direction one row down
	 * 
	 * @param xPos  x position of lever
	 */
	
	public void popLever(int xPos)
	{
		int yCounter;
		int ySize=getYSize()-1;
		
		for (yCounter=0;yCounter<ySize;yCounter++)
			{
			setPixel(xPos,yCounter,getPixel(xPos,yCounter+1));
			}
			setPixel(xPos,ySize,(short)0);
	}


	/**
	 * @author Tavin
	 * 
	 * Date 2011.05.16
	 * 
	 * Push lever on given x position, lever an all balls are moved in y direction one row up
	 * 
	 * @param xPos
	 */
	
	public void pushLever(int xPos)
	{
		int yCounter;
		int ySize=getYSize()-2;
		
		for (yCounter=ySize;yCounter>0;yCounter--)
			{
			setPixel(xPos,yCounter,getPixel(xPos,yCounter-1));
			}
		setPixel(xPos,0,LEVER_COLOR);
	}

	/**
	 * @author Tavin
	 * 
	 * Date 2011.05.16
	 * 
	 * Check if there are some ball matches, remove balls if there were more then 2 following balls with the same color in one row 
	 * 
	 * @return points
	 */
	
	
	public int checkMatches()
	{
		int xCounter;
		int yCounter;
		short value;
		int points=0;
		
		for (yCounter=0;yCounter<getYSize();yCounter++)
			for (xCounter=1;xCounter<getXSize()-1;xCounter++){
				
				value= getPixel(xCounter,yCounter);
				
				if ((value !=0) && (value !=LEVER_COLOR))
					{
					if ((getPixel(xCounter-1,yCounter)==value) & (getPixel(xCounter+1,yCounter)==value))
							{
							setPixel(xCounter-1,yCounter,(short)0);
							setPixel(xCounter++,yCounter,(short)0);
							setPixel(xCounter++,yCounter,(short)0);
							
							if (points==0)
								points=3;
							else {
								points*=3;
								}
							
							while (getPixel(xCounter,yCounter)==value){
								setPixel(xCounter++,yCounter,(short)0);
								points*=2;
	
							}
						}
					}
				
			}
		return(points);
	}
	
	
	
	
}
